<?php

/**
 * 附件上传
 */
namespace Asset\Controller;
use Common\Controller\AdminbaseController;
class AssetController extends AdminbaseController {


    function _initialize() {
    	$adminid=sp_get_current_admin_id();
    	$userid=sp_get_current_userid();
    	if(empty($adminid) && empty($userid)){
    		exit("非法上传！");
    	}
    }

    /**
     * swfupload 上传 
     */
    public function swfupload() {
        if (IS_POST) {
			$savepath=date('Ymd').'/';
            //上传处理类
            $config=array(
            		'rootPath' => './'.C("UPLOADPATH"),
            		'savePath' => $savepath,
            		'maxSize' => 11048576,
            		'saveName'   =>    array('uniqid',''),
            		'exts'       =>    array('jpg', 'gif', 'png', 'jpeg',"txt",'zip'),
            		'autoSub'    =>    false,
            );
			$upload = new \Think\Upload($config);
			$info=$upload->upload();
            //开始上传
            if ($info) {
				// 上传成功
				// TODO xianfei  写入附件数据库信息
				$first = array_shift($info);
				if (! empty($first['url'])) {
					$url = $first['url'];
				} else {
					$url = C("TMPL_PARSE_STRING.__UPLOAD__") . $savepath . $first['savename'];
				}
				
				echo "1," . $url . "," . '1,' . $first['name'];
				exit();
			} else {
				// 上传失败，返回错误
				exit("0," . $upload->getError());
			}
        } else {
            $this->display(':swfupload');
        }
    }
    
    /**
     * swfupload 上传
     */
    public function swfuploadfile() {
    	if (IS_POST) {
    		$savepath = date('Ymd').'/';
    		//上传处理类
    		$config=array(
    			'rootPath' => './'.C("UPLOADPATH"),
    			'savePath' => $savepath,
    			'maxSize' => 11048576,
    			'saveName'   =>    array('uniqid',''),
		    	'exts'  	 =>    array('jpg', 'gif', 'png', 'jpeg','txt','pdf','doc','docx','xls','xlsx','zip','rar','ppt','pptx',),
    			'autoSub'    =>    false,
    		);
    		$upload = new \Think\Upload($config);
    		//开始上传
    		$info = $upload->upload();
    		if ($info) {
				// 上传成功
				// TODO xianfei 写入附件数据库信息
				
				$first = array_shift($info);
				if (! empty($first['url'])) {
					$url = $first['url'];
				} else {
					$url = C("TMPL_PARSE_STRING.__UPLOAD__") . $savepath . $first['savename'];
				}
				
				echo "1," . $url . "," . '1,' . $first['name'];
				exit();
			} else {
				// 上传失败，返回错误
				exit("0," . $upload->getError());
			}
    	} else {
    		
    		$catid = explode(",",I('catid'));    		
    		$args = explode(",",I('args'));
    		$file_types = explode("|", $args[1]);
    		foreach ($file_types as &$file_types_v) {
    			$file_types_v = "*." . $file_types_v;
    		}
    		$file_types = implode(';', $file_types);
    		
    		$this->assign('file_types', $file_types);
    		$this->assign('filetype_post', $args[1]);
    		$this->assign('file_upload_limit', $args[0]);
    		$this->assign('file_size_limit', ini_get('upload_max_filesize'));
    		
    		$this->display(':swfuploadfile');
    	}
    }
    

}
